package com.kensure.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import co.kensure.frame.ResultInfo;
import co.kensure.frame.ResultRowInfo;
import co.kensure.frame.ResultRowsInfo;
import co.kensure.http.RequestUtils;
import co.kensure.mem.PageInfo;

import com.alibaba.fastjson.JSONObject;
import com.kensure.shike.baobei.model.SKBaobei;
import com.kensure.shike.baobei.model.SKBaobeiCB;
import com.kensure.shike.baobei.model.SKBaobeiSku;
import com.kensure.shike.baobei.model.SKType;
import com.kensure.shike.baobei.query.SKBaobeiHTQuery;
import com.kensure.shike.baobei.query.SKBaobeiQuery;
import com.kensure.shike.baobei.service.SKBaobeiCBService;
import com.kensure.shike.baobei.service.SKBaobeiService;
import com.kensure.shike.baobei.service.SKBaobeiSkuService;
import com.kensure.shike.changshang.model.ChangShang;
import com.kensure.shike.changshang.service.ChangShangService;
import com.kensure.shike.user.model.SKLogin;
import com.kensure.shike.user.service.SKLoginService;

/**
 * 宝贝商品逻辑
 * 
 * @author fankaidi
 *
 */
@Controller
@RequestMapping(value = "baobei")
public class SKBaobeiController {

	@Resource
	private SKBaobeiService sKBaobeiService;
	@Resource
	private SKBaobeiSkuService sKBaobeiSkuService;
	@Resource
	private SKBaobeiCBService sKBaobeiCBService;
	@Resource
	private SKLoginService sKLoginService;
	@Resource
	private ChangShangService changShangService;

	/**
	 * 前端展现商品
	 */
	@ResponseBody
	@RequestMapping(value = "qdlist.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo qdlist(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		PageInfo page = JSONObject.parseObject(json.toJSONString(), PageInfo.class);
		if(page.getPageSize() == 20){
			page.setPageSize(1000);
		}
		SKBaobeiQuery query = JSONObject.parseObject(json.toJSONString(), SKBaobeiQuery.class);
		List<SKBaobei> list = sKBaobeiService.qdlist(query, page);
		return new ResultRowsInfo(list);
	}

	/**
	 * 前端展现商品，详细
	 */
	@ResponseBody
	@RequestMapping(value = "getfull.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo getfull(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		Long id = json.getLong("id");
		SKBaobei baobei = sKBaobeiService.selectFull(id);
		return new ResultRowInfo(baobei);
	}

	/**
	 * 前端首页商品分类
	 */
	@ResponseBody
	@RequestMapping(value = "sylist.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo sylist(HttpServletRequest req, HttpServletResponse rep) {
		List<SKType> list = sKBaobeiService.sylist();
		return new ResultRowsInfo(list);
	}

	/**
	 * 后端展现商品
	 */
	@ResponseBody
	@RequestMapping(value = "hdlist.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo hdlist(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		SKBaobeiHTQuery query = JSONObject.parseObject(json.toJSONString(), SKBaobeiHTQuery.class);
		PageInfo page = JSONObject.parseObject(json.toJSONString(), PageInfo.class);
		List<SKBaobei> list = sKBaobeiService.htlist(query, page);
		Long cnt = sKBaobeiService.htlistCount(query);
		return new ResultRowsInfo(list, cnt);
	}
	
	/**
	 * 商家端展现商品
	 */
	@ResponseBody
	@RequestMapping(value = "sjlist.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo sjlist(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		String tokenid = json.getString("tokenid");
		SKLogin login = sKLoginService.selectByTokenId(tokenid);
		List<ChangShang> changshang = changShangService.selectByUserId(login.getUserid());
		Long sjid = changshang.get(0).getId();
		
		SKBaobeiHTQuery query = JSONObject.parseObject(json.toJSONString(), SKBaobeiHTQuery.class);
		PageInfo page = JSONObject.parseObject(json.toJSONString(), PageInfo.class);
		query.setSjid(sjid);
		List<SKBaobei> list = sKBaobeiService.sjlist(query, page);
		Long cnt = sKBaobeiService.sjlistCount(query);
		return new ResultRowsInfo(list, cnt);
	}

	/**
	 * 后端商品保存
	 */
	@ResponseBody
	@RequestMapping(value = "save.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo save(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		json.remove("size");
		SKBaobei baobei = JSONObject.parseObject(json.toJSONString(), SKBaobei.class);
		sKBaobeiService.save(baobei);
		return new ResultRowInfo(baobei.getId());
	}

	/**
	 * 后端商品修改库存
	 */
	@ResponseBody
	@RequestMapping(value = "updatekucun.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo updatekucun(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		Long bbid = json.getLong("id");
		List<SKBaobeiSku> skus = JSONObject.parseArray(json.getString("skus"), SKBaobeiSku.class);
		sKBaobeiSkuService.updateKuCun(skus, bbid);
		return new ResultRowInfo(bbid);
	}

	/**
	 * 后端商品上架
	 */
	@ResponseBody
	@RequestMapping(value = "sj.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo sj(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		Long id = json.getLong("id");
		sKBaobeiService.shangjia(id);
		return new ResultRowInfo(id);
	}

	/**
	 * 后端商品下架
	 */
	@ResponseBody
	@RequestMapping(value = "xj.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo xj(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		Long id = json.getLong("id");
		sKBaobeiService.xiajia(id);
		return new ResultRowInfo(id);
	}

	/**
	 * 修改商品成本
	 */
	@ResponseBody
	@RequestMapping(value = "updatecb.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo updatecb(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		SKBaobeiCB newcb = JSONObject.parseObject(json.toJSONString(), SKBaobeiCB.class);
		sKBaobeiCBService.save(newcb);
		return new ResultRowInfo();
	}
}
